Skip to content

读取结果集 - ExecuteReader

函数简介

执行指定的SQL查询语句,返回一个STMT指针,用于遍历查询结果集。

接口名称

ExecuteReader

DLL调用

c
long ExecuteReader(long ola, long db, string sql);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
db长整数型数据库连接句柄,由 OpenDatabase 接口生成。
sql字符串要执行的SQL查询语句。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long db = ola.OpenDatabase("data/app.db", "");
if (db != 0) {
    ola.ExecuteSql(db, "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, score REAL)");
    ola.ExecuteSql(db, "INSERT OR REPLACE INTO users VALUES(1,'ola',98.5)");
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    if (stmt != 0) {
        ola.Finalize(stmt);
    }
    ola.CloseDatabase(db);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long db = ola.OpenDatabase("data/app.db", "");
if (db != 0)
{
    ola.ExecuteSql(db, "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, score REAL)");
    ola.ExecuteSql(db, "INSERT OR REPLACE INTO users VALUES(1,'ola',98.5)");
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    if (stmt != 0)
    {
        ola.Finalize(stmt);
    }
    ola.CloseDatabase(db);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
db = ola.OpenDatabase("data/app.db", "")
if db != 0:
    ola.ExecuteSql(db, "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, score REAL)")
    ola.ExecuteSql(db, "INSERT OR REPLACE INTO users VALUES(1,'ola',98.5)")
    stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1")
    if stmt != 0:
        ola.Finalize(stmt)
    ola.CloseDatabase(db)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long db = ola.OpenDatabase("data/app.db", "");
if (db != 0) {
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    ola.CloseDatabase(db);
}
cpp
var ola = com("OlaPlug.OlaSoft")
var db = ola.OpenDatabase("data/app.db", "")
if(db) {
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    ola.CloseDatabase(db)
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
db = ola.OpenDatabase("data/app.db", "")
If db <> 0 Then
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    ola.CloseDatabase(db)
End If
text
.局部变量 ola, OLAPlug
ola.创建 ()
db = ola.OpenDatabase (“data/app.db“, ““)
.如果真 (db ≠ 0)
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    ola.CloseDatabase (db)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var db = ola.OpenDatabase("data/app.db", "");
if(db){
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    ola.CloseDatabase(db);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 db = ola.OpenDatabase("data/app.db", "")
如果真 (db ≠ 0)
{
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    ola.CloseDatabase(db)
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long db = ola.OpenDatabase("data/app.db", "");
if (db != 0) {
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    ola.CloseDatabase(db);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long db = OpenDatabase(instance, "data/app.db", "");
if (db != 0) {
    ola.ExecuteSql(db, "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, score REAL)");
    ola.ExecuteSql(db, "INSERT OR REPLACE INTO users VALUES(1,'ola',98.5)");
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    if (stmt != 0) {
        ola.Finalize(stmt);
    }
    CloseDatabase(instance, db);
}
csharp
using System.Runtime.InteropServices;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
long db = OpenDatabase(instance, "data/app.db", "");
if (db != 0)
{
    ola.ExecuteSql(db, "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, score REAL)");
    ola.ExecuteSql(db, "INSERT OR REPLACE INTO users VALUES(1,'ola',98.5)");
    long stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1");
    if (stmt != 0)
    {
        ola.Finalize(stmt);
    }
    CloseDatabase(instance, db);
}
python
from ctypes import CDLL, c_int, c_int64

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
db = ola.OpenDatabase(instance, b"data/app.db", b"")
if db:
    ola.ExecuteSql(db, "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, score REAL)")
    ola.ExecuteSql(db, "INSERT OR REPLACE INTO users VALUES(1,'ola',98.5)")
    stmt = ola.ExecuteReader(db, "SELECT id, name, score FROM users WHERE id=1")
    if stmt != 0:
        ola.Finalize(stmt)
    ola.CloseDatabase(instance, db)

返回值

STMT指针,用于遍历查询结果集。如果查询失败,返回 0。

注意事项

  • 该函数适用于需要逐行读取查询结果的场景,返回的STMT指针可配合 Read 遍历结果集。
  • 使用完STMT指针后,应调用 Finalize 释放资源,避免内存泄漏。